package by.alex.dzeshko.facebooksuperclient.database;

import org.json.JSONObject;
import by.alex.dzeshko.facebooksuperclient.R;
import by.alex.dzeshko.facebooksuperclient.api.ApiRequest;
import by.alex.dzeshko.facebooksuperclient.models.FbUser;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DbUsersHelper extends DbManager {
	
	public DbUsersHelper(Context context, CursorFactory factory) {
		super(context, factory);
	}
	
	public Cursor getItems() {
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query(USER_PROFILE_TABLE, null, null, null, null, null, null);
		return cursor;
	}

	@Override	
	public long addItem(JSONObject jsonObject) {
		SQLiteDatabase db = getWritableDatabase();
		long value;
		ContentValues values = this.getContentValues(jsonObject);
		value = db.insertWithOnConflict(USER_PROFILE_TABLE, null, values,SQLiteDatabase.CONFLICT_REPLACE);
		return value; 	
	}

	@Override
	protected ContentValues getContentValues(JSONObject pItem) {
		FbUser user = new FbUser(pItem);
		ContentValues values = new ContentValues();
		values.put(USER_PROFILE_COLUMNS[ID_COL_INDEX], user.getId());
		values.put(USER_PROFILE_COLUMNS[NAME_COL_INDEX], user.getName());
		values.put(USER_PROFILE_COLUMNS[USER_NAME_COL_INDEX], user.getUser_name());
		values.put(USER_PROFILE_COLUMNS[BIRTHDAY_COL_INDEX], user.getBirthday());
		values.put(USER_PROFILE_COLUMNS[HOMETOWN_COL_INDEX], user.getHometown());
		values.put(USER_PROFILE_COLUMNS[LOCATION_COL_INDEX], user.getLocation());
		values.put(USER_PROFILE_COLUMNS[GENDER_COL_INDEX], user.getGender());
		values.put(USER_PROFILE_COLUMNS[RELATIONSHIP_STATUS_COL_INDEX], user.getRelationship_status());
		values.put(USER_PROFILE_COLUMNS[RELIGION_COL_INDEX], user.getReligion());
		values.put(USER_PROFILE_COLUMNS[POLITICAL_COL_INDEX], user.getPolitical());
		String id = String.valueOf(user.getId());
		values.put(USER_PROFILE_COLUMNS[PICTURE_URL_COL_INDEX], ApiRequest.getProfilePicture(id, 150, 150));
		return values;
	}
}


